from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from ..database import Base

class ChatHistory(Base):
    __tablename__ = "chat_histories"
    
    id = Column(Integer, primary_key=True, index=True)
    session_id = Column(String(100), index=True)  # 会话ID
    user_message = Column(Text, nullable=False)
    ai_response = Column(Text, nullable=False)
    message_type = Column(String(20), default="chat")  # chat, qa, writing等
    
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # 外键
    user_id = Column(Integer, ForeignKey("users.id"))
    
    # 关系
    user = relationship("User", back_populates="chat_histories") 